<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>SIP参数</span>
        <!--<el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>-->
      </div>
      <div class="text item">
        <el-form ref="form" :rules="rules" size="small" :model="form" label-width="150px">
          <el-form-item label="本端SIP代理地址" prop="localIp">
            <el-col :xs="12" :sm="8" :md="8" :lg="5" :xl="3">
              <el-input v-model="form.localIp"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="本端SIP代理端口" prop="localPort">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.localPort"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="SIP代理注册用户ID" prop="sipUAUserId">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.sipUAUserId"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="SIP代理注册用户名" prop="sipUAUserName">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.sipUAUserName"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="SIP代理注册密码" prop="sipUAPassword">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.sipUAPassword"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="SIP注册服务器地址" prop="sipServerIp">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.sipServerIp"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="SIP注册服务器端口" prop="sipServerPort">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.sipServerPort"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="ICE服务器地址" prop="iceServerIp">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.iceServerIp"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="ICE服务器端口" prop="iceServerPort">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model.number="form.iceServerPort"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="ICE登录用户名">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.iceUserName"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="ICE登录密码">
            <el-col :xs="12" :sm="8" :md="6" :lg="5" :xl="3">
              <el-input v-model="form.icePassword"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="主动呼叫的号码" prop="peerUserId">
            <el-col :xs="12" :sm="8" :md="6" :lg="4" :xl="3">
              <el-input v-model="form.peerUserId"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="setSipUAInfo">保存</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <restart-tool ref="restartOpt"></restart-tool>
  </div>
</template>

<script>
  import restartTool from '../public/restartConfirm'
    export default {
        name: "sipParameters",
        components:{
          'restart-tool':restartTool
        },
        data() {
          var regPort=/^[0-9]+$/
          var requiredPort = (rule, value, callback) => {

            // debugger
            if (value === '') {
              callback(new Error('请输入端口号'));
            }else if(!regPort.test(value)){
              callback(new Error('端口号只能输入数字'));
            } else if(value<0||value>65535){
              callback(new Error('端口号只能输入 0 ~ 65535 之间的数字'));
            } else{
              callback();
            }
          };
          var requiredData=(rule, value, callback)=>{
            if(value===''){
              callback(new Error('必填项，不能为空！'));
            }else{
              callback();
            }
          }
          var checkIp=(rule, value, callback)=>{
            if(value===''){
              callback(new Error('必填项，不能为空！'));
            }else if(!this.BASE.validateItem.ip.test(value)){
              callback(new Error('IP格式不正确！'));
            }else{
              callback();
            }
          }
          var checkIceServerIp=(rule, value, callback)=>{
            if(value!=''&&!this.BASE.validateItem.ip.test(value)){
              callback(new Error('IP格式不正确！'));
            }else{
              callback();
            }
          }
          var checkIceServerPort=(rule, value, callback)=>{
            if(value!=''&&!regPort.test(value)){
              callback(new Error('端口号只能输入数字'));
            }else if(value!=''&&(value<0||value>65535)){
              callback(new Error('端口号只能输入 0 ~ 65535 之间的数字'));
            }else{
              callback();
            }
          }
          return {
            form: {
              localIp: '',
              localPort: 80,
              sipUAUserId: '',
              sipUAUserName: '',
              sipUAPassword: '',
              sipServerIp: '',
              sipServerPort: 80,
              iceServerIp:'',
              iceServerPort:80,
              iceUserName: '',
              icePassword: '',
              peerUserId: '',
            },
            rules: {
              localIp: [
                {required: true, validator: checkIp, trigger: 'blur' }
              ],
              localPort: [
                {required: true, validator: requiredPort, trigger: 'blur' }
              ],
              sipUAUserId: [
                {required: true, validator: requiredData, trigger: 'blur' }
              ],
              sipUAUserName: [
                {required: true, validator: requiredData, trigger: 'blur' }
              ],
              sipUAPassword: [
                {required: true, validator: requiredData, trigger: 'blur' }
              ],
              peerUserId: [
                {required: true, validator: requiredData, trigger: 'blur' }
              ],
              sipServerIp: [
                {required: true, validator: checkIp, trigger: 'blur' }
              ],
              sipServerPort: [
                {required: true, validator: requiredPort, trigger: 'blur' }
              ],
              iceServerIp: [
                { validator: checkIceServerIp, trigger: 'blur' }
              ],
              iceServerPort: [
                { validator: checkIceServerPort, trigger: 'blur' }
              ],
            }
          }
        },
        methods: {
          setSipUAInfo() {
            this.$refs['form'].validate((valid) => {
              if (valid) {
                var _this=this
                _this.loading=true
                _this.postAjax({
                  method:'setSipUAInfo',
                  params:{
                    localIp:_this.form.localIp,
                    localPort:_this.form.localPort*1,
                    sipUAUserId:_this.form.sipUAUserId,
                    sipUAUserName:_this.form.sipUAUserName,
                    sipUAPassword:_this.form.sipUAPassword,
                    sipServerIp:_this.form.sipServerIp,
                    sipServerPort:_this.form.sipServerPort*1,
                    iceServerIp:_this.form.iceServerIp,
                    iceServerPort:_this.form.iceServerPort*1,
                    iceUserName:_this.form.iceUserName,
                    icePassword:_this.form.icePassword,
                    peerUserId:_this.form.peerUserId
                  }
                }).then(function (res) {
                  console.log(res);
                  if(res.result==0){
                    _this.tips('保存成功','success')

                    setTimeout(function () {
                      _this.$confirm('更改成功，重启后生效?', '提示', {
                        confirmButtonText: '立即重启',
                        cancelButtonText: '稍后重启',
                        type: 'warning'
                      }).then(() => {
                        console.log('即将显示重启窗口');
                        _this.$refs.restartOpt.restart()
                        // _this.dialogVisible=true
                        // _this.$message({
                        //   type: 'success',
                        //   message: '立即重启!'
                        // });
                      }).catch(() => {
                        _this.form.oldip=_this.form.ip
                        // _this.$message({
                        //   type: 'info',
                        //   message: '稍后重启'
                        // });
                        console.log('oldip',_this.form.oldip);
                      });
                    },500)

                    // var data=res.params
                    // // debugger
                    // _this.form.ip=data.ip
                    // _this.form.gateway=data.gateway
                    // _this.form.mask=data.mask
                    // _this.form.dns1=data.dns1
                    // _this.form.dns2=data.dns2
                  }else {
                    _this.tips(res.errorInfo?res.errorInfo.detail:res,'error')
                  }
                }).catch(function (err) {
                  console.log(err);
                }).finally(function (fn) {
                  _this.loading=false
                  console.log(fn);
                })
              }else{
                return false;
              }
            })
          },
          getSipUAInfo(){
            var _this=this
            _this.postAjax({
              method:'getSipUAInfo',
              params:{}
            }).then(function (res) {
              console.log(res);
              if(res.result==0){
                var data=res.params
                _this.form.localIp=data.localIp,
                  _this.form.localPort=data.localPort,
                  _this.form.sipUAUserId=data.sipUAUserId,
                  _this.form.sipUAUserName=data.sipUAUserName,
                  _this.form.sipUAPassword=data.sipUAPassword,
                  _this.form.sipServerIp=data.sipServerIp,
                  _this.form.sipServerPort=data.sipServerPort,
                  _this.form.iceServerIp=data.iceServerIp,
                  _this.form.iceServerPort=data.iceServerPort,
                  _this.form.iceUserName=data.iceUserName,
                  _this.form.icePassword=data.icePassword,
                  _this.form.peerUserId=data.peerUserId
              }else{
                _this.tips(res.errorInfo.detail,'error')
              }
            }).catch(function (err) {
              console.log(err);
            }).finally(function (fn) {
              console.log(fn);
            })
          }
        },
      mounted(){
          this.getSipUAInfo()
      }
    }
</script>

<style scoped>
  .el-form-item{
    /*margin-bottom: 10px;*/
  }
</style>
